<?php if ( !defined('BASEPATH')) exit('No direct script acess allowed');

class banner extends CI_Controller{


    	function __construct(){
		parent::__construct();
		
		//if (($this->session->userdata('status')) <> "logado"){
                //echo '<script language="JavaScript"> window.location="'.(base_url('administracao')).'"; </script>'; 
                //}
        }

public function index($pg = 0, $data = null){
        
            $this->load->model('cbanner');
        
            $pg = $pg -1;
            $numreg = 5; // Quantos registros por página vai ser mostrado
            $inicial = ($pg * 5); 
            $data['registro'] = $this->cbanner->get_all_banners_com_paginacao($inicial,$numreg);
            $data['quantreg'] = $this->cbanner->conta_registro_banners();
            $data['numreg'] = $numreg;
            $data['pgg'] = $pg;
        
            $this->load->view('meu_painel_de_administracao/require_padrao/topo');
            $this->load->view('meu_painel_de_administracao/require_padrao/meus_dados');
            $this->load->view('meu_painel_de_administracao/require_padrao/menu');
            $this->load->view('meu_painel_de_administracao/view_banner',$data);
            $this->load->view('meu_painel_de_administracao/require_padrao/rodape');            
            
}
        
      

        
public function salva_novo($pg = 0, $data = null){  
    
    $this->load->model('cbanner');
        
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules('nomeban','Nome do Banner','required|trim|max_length[45]');  //nome ok
        $this->form_validation->set_rules('posicao','Posição','required|trim');                         //posi ok

        
        /* ---------- ------- ----- ROTINA DE UPLOAD -------- ------ ------- -------  */
        $update = true;
        $config['upload_path'] = './assets/img_banner';
        $config['allowed_types'] = 'jpg|png|gif|JPEG';
        $config['encrypt_name'] = TRUE;
        $config['max_width']  = '4000';
        $config['max_height']  = '4000';
        $config['max_size']   = '90000000000000000000000000000000000000000000';
        //$config['remove_spaces'] = TRUE;
        $this->load->library('upload',$config);
        if(!$this->upload->do_upload()){
            $error = array('errado' => $this->upload->display_errors());
            $_SESSION['msg422'] = $error['errado'];
            $update = false;
        }
        /* ---------- ------------ FIM ROTINA DE UPLOAD ---------- ------------ */
       
        if ($this->form_validation->run() == FALSE || $update == false){
            

            
            $data['nnomeban']               = $this->input->post('nomeban');             
            $data['nposicao']               = $this->input->post('posicao'); 
            $data['nstatus']               = $this->input->post('status'); 
            $data['bt_novo']                = 1;

            $this->index($pg, $data);
                
        }else{

            $file_data = $this->upload->data();
            
            $new_banner['ban_nome']                = $this->input->post('nomeban');
            $new_banner['ban_posicao']             = $this->input->post('posicao');
            $new_banner['ban_status']              = $this->input->post('status');
            $new_banner['ban_img']                 = $file_data['file_name'];


            if($this->cbanner->adicionar_novo_banner($new_banner) == true){

                 $this->session->set_flashdata('mensagemok','Banner cadastrado com sucesso !');
                 echo '<script language="JavaScript"> window.location="'.(base_url('meu_painel_de_administracao/banner/index/'.$pg)).'"; </script>'; 

            }else{

                 $this->session->set_flashdata('mensagemerro','Erro, tente novamente !');
                 echo '<script language="JavaScript"> window.location="'.(base_url('meu_painel_de_administracao/banner/index/'.$pg)).'"; </script>'; 
            }             
            
        }

}      
        
        
public function solicita_dados_editar($pg = 0, $id = 0) {
    
    $this->load->model('cbanner');
    
    $banner = $this->cbanner->solicita_dados($id);
    
        foreach ($banner as $ban){
    
            $data['enomeban']               = $ban->ban_nome ;            
            $data['eposicao']               = $ban->ban_posicao ; 
            $data['estatus']                = $ban->ban_status ;
            $data['eimg']                   = $ban->ban_img ;
            $data['eid']                    = $ban->ban_id ;
            $data['bt_novo']                = 2;

            $this->index($pg, $data);
        }

}     
        
public function salva_altracao($pg = 0, $id = 0){  
    
    $this->load->model('cbanner');
        
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules('nomeban','Nome do Banner','required|trim|max_length[45]');  //nome ok
        $this->form_validation->set_rules('posicao','Posição','required|trim');                        //posi ok

        
        if ($this->form_validation->run() == FALSE){
            
            $data['enomeban']               = $this->input->post('nomeban');             
            $data['eposicao']               = $this->input->post('posicao'); 
            $data['estatus']                = $this->input->post('status'); 
            $data['eimg']                   = $this->input->post('img');
            $data['eid']                    = $id;
            $data['bt_novo']                = 2;

            $this->index($pg, $data);
                
        }else{
            
            $new_banner['ban_nome']                = $this->input->post('nomeban');
            $new_banner['ban_posicao']             = $this->input->post('posicao');
            $new_banner['ban_status']              = $this->input->post('status');


            if($this->cbanner->alterar_banner($new_banner,$id) == true){

                 $this->session->set_flashdata('mensagemok','Banner alterado com sucesso !');
                 echo '<script language="JavaScript"> window.location="'.(base_url('meu_painel_de_administracao/banner/index/'.$pg)).'"; </script>'; 

            }else{

                 $this->session->set_flashdata('mensagemerro','Erro, tente novamente !');
                 echo '<script language="JavaScript"> window.location="'.(base_url('meu_painel_de_administracao/banner/index/'.$pg)).'"; </script>'; 
            }             
            
        }

}      
                
        
public function exclui_banner($pg = 0, $id = 0){  
    
    $this->load->model('cbanner');
    
    $file =  $this->cbanner->retorna_nome_da_img($id);
    
    if(file_exists("./assets/img_banner/".$file[0]->ban_img)){

            @unlink("./assets/img_banner/".$file[0]->ban_img);  
    }
    
    if($this->cbanner->exclui_banner($id) == true){
        
        $this->session->set_flashdata('mensagemok','Banner excluido com sucesso !');
        echo '<script language="JavaScript"> window.location="'.(base_url('meu_painel_de_administracao/banner/index/'.$pg)).'"; </script>'; 

   }else{

        $this->session->set_flashdata('mensagemerro','Erro, tente novamente !');
        echo '<script language="JavaScript"> window.location="'.(base_url('meu_painel_de_administracao/banner/index/'.$pg)).'"; </script>'; 
   }   
}


    
public function confirma_excluir_banner($pg,$id){
 
 $array['msg'] = '   
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
decisao = confirm("Deseja mesmo excluir este banner ?\n\
-Ao excluir este banner, a imagem será deletada do sistema.");

if (decisao){
    //ok
window.location="'.(base_url('meu_painel_de_administracao/banner/exclui_banner/'.$pg.'/'.$id)).'";
} else {
    //cancelar
window.location="'.(base_url('meu_painel_de_administracao/banner/index/'.$pg)).'";
}
</SCRIPT>
';

    $this->load->view('meu_painel_de_administracao/require_padrao/conteudo_confirmacao',$array);
}   







        
        
    
    
    
    
}
